package com.epam.utils.filters;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RegAuthorizationFilter implements Filter {

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		Principal principal = request.getUserPrincipal();
		if (principal == null) {
			filterChain.doFilter(req, response);
		} else {
			((HttpServletResponse)response).sendRedirect("error.xhtml?message=logout+first");
		}
	}

	@Override
	public void destroy() {
	}

}
